<?php

namespace app\bll;

use app\validate\OrganPost as ValidateOrganPost;
use Doctrine\DBAL\Query;
use Exception;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use support\Db;

/**
 * 组织架构-岗位
 */
class OrganPost
{
    /**
     * 获取一条记录
     *
     * @param integer $id
     * @return array
     */
    public function find(int $id): array
    {
        $data =  Db::table('frame_organ_post')
            ->where('id', '=', $id)
            ->first();
        return $data === null ? [] : get_object_vars($data);
    }

    /**
     *  查询
     *
     * @param string $title
     * @return array
     */
    public function select(string $title = ''): array
    {
        return Db::table('frame_organ_post')
            ->when($title !== '', function ($query) use ($title) {
                return $query->where('title', 'like', '%' . $$title . '%');
            })
            ->get()
            ->map(function ($value) {
                return (array)$value;
            })
            ->toArray();
    }

    /**
     * 添加
     *
     * @param array $data
     * @return integer
     */
    public function add(array $data): int
    {
        $ValidateOrganPost = new ValidateOrganPost();
        if (!$ValidateOrganPost->check($data)) {
            throw new Exception($ValidateOrganPost->getSimpleError());
        }

        return  Db::table('frame_organ_post')
            ->insertGetId($data);
    }


    /**
     * 编辑
     *
     * @param integer $id
     * @param array $data
     * @return integer
     */
    public function edit(int $id, array $data): int
    {

        $ValidateOrganPost = new ValidateOrganPost();
        if (!$ValidateOrganPost->check($data)) {
            throw new Exception($ValidateOrganPost->getSimpleError());
        }

        return  Db::table('frame_organ_post')
            ->where('id', '=', $id)
            ->update($data);
    }

    /**
     * 删除岗位
     *
     * @param integer $id
     * @return void
     */
    public function delete(int $id)
    {
        Db::beginTransaction();
        try {

            Db::table('frame_organ_post')
                ->where('id', '=', $id)
                ->delete();

            Db::table('frame_user_basicinfo')
                ->where('organpostid', '=', $id)
                ->update([
                    'organpostid' => null
                ]);

            Db::commit();
        } catch (Exception $ex) {
            Db::rollBack();
            throw $ex;
        }
    }
    /**
     * 分页查询
     *
     * @param string $name
     * @param string $ident
     * @param integer $pagesize
     * @return LengthAwarePaginator
     */
    public function paginator(
        string $title = '',
        int $pagesize = 20
    ): LengthAwarePaginator {
        return  Db::table('frame_organ_post')
            ->when($title != '', function ($query) use ($title) {
                return $query->where('title', 'like', '%' . $title . '%');
            })
            ->orderBy('id', 'desc')
            ->paginate($pagesize);
    }
}
